<?php

namespace app\api\controller;

use app\api\model\AdminConfigs;
use app\api\model\PayCommon;
use Illuminate\Support\Carbon;
use support\Request;

class AdminConfigsController
{

    public function getAdminInfo(Request $request){
        $res = AdminConfigs::first();
        return json([
            'code' => 2,
            'msg' => '获取成功！',
            'data' => $res,
        ]);

    }
    /**
     * @param Request $request
     * @param AdminConfigs $DB
     * @return \support\Response
     * 添加母鸡卡模板
     */
    public function add(Request $request, AdminConfigs $DB)
    {
        $id = $request->post('id');
        $req = $request->only(['customer_wechat', 'customer_qq', 'customer_phone', 'invite_friend_count', 'friend_buy_chicken_card_count', 'friend_produce_egg_count',
            'reward_egg_count', 'pot_daily_commission_rate', 'lottery_winner_num']);
        $res = $DB->where('id', $id)->first();
        $req['created_at'] = Carbon::now()->toDateTimeString();
        if (!$res) {
            $res = $DB->insertGetId($req);
            $code = 2;
            $msg = '添加成功!';
        } else {
            $code = 0;
            $msg = '模板已存在，请修改';
        }
        return json([
            'code' => $code,
            'msg' => $msg,
            'data' => $res,
        ]);
    }

    /**
     * @param Request $request
     * @param AdminConfigs $DB
     * @return \support\Response
     * 修改信息
     */
    public function update(Request $request, AdminConfigs $DB)
    {
        $req = $request->only(['id','customer_wechat', 'customer_qq', 'customer_phone', 'invite_friend_count', 'friend_buy_chicken_card_count', 'friend_produce_egg_count',
            'reward_egg_count', 'pot_daily_commission_rate', 'lottery_winner_num']);
        $req['updated_at'] = Carbon::now()->toDateTimeString();
        $res = $DB->where('id', $req['id'])->update($req);
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }


    public function pay_type()
    {
        $res = PayCommon::get();
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }

}
